package com.weavernorth.performance;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.workflow.webservices.*;

public class ManagerCRMFlow {


    //创建店长  CRM流程
    public void  createRetail (){
        BaseBean bb = new BaseBean();
        RecordSet rsShop = new RecordSet();
        RecordSet rsShopTwo = new RecordSet();
        RecordSet rsGrade = new RecordSet();
        RecordSet rsClerk = new RecordSet();
        RecordSet rsClerkNum = new RecordSet();
        RecordSet rsDc = new RecordSet();
        RecordSet rsPfr = new RecordSet();
        //查询店铺信息表获取数据
        String strShopsql ="select fgs  from uf_dpxx  where ( sfdpphd=1  )  group by fgs  ";
        bb.writeLog("获取店铺信息的sql:"+strShopsql);
        rsShop.executeQuery(strShopsql);
        while(rsShop.next()){
            //获取分公司的值
            String strFgs = Util.null2String(rsShop.getString("fgs"));
            bb.writeLog("分公司的值："+strFgs);

            //获取店铺信息其他数据的sql
            String strShopSqlTwo="select dc,ppkdx,dpzt,dpjb,dp,lsjl from uf_dpxx where   ( sfdpphd=1  )  and  fgs='"+strFgs+"' and dc is not null";
            bb.writeLog("获取店铺信息其他数据的sql："+strShopSqlTwo);
            rsShopTwo.executeQuery(strShopSqlTwo);
            String strBrand="";
            String strStauts="";
            String strRank="";
            String strDpName="";
            //区域经理
            String strLsjl="";
            if(rsShopTwo.next()) {
                //品牌
                strBrand = Util.null2String(rsShopTwo.getString("ppkdx"));
                bb.writeLog("品牌："+strBrand);
                //获取店铺状态
                strStauts = Util.null2String(rsShopTwo.getString("dpzt"));
                bb.writeLog("店铺状态："+strStauts);
                //获取店铺级别
                strRank = Util.null2String(rsShopTwo.getString("dpjb"));
                bb.writeLog("店铺级别："+strRank);
                //店铺名称
                strDpName = Util.null2String(rsShopTwo.getString("dp"));
                bb.writeLog("店铺名称："+strDpName);


                //获取区域经理
                strLsjl = Util.null2String(rsShopTwo.getString("lsjl"));
              bb.writeLog("零售经理的值："+strLsjl);
            }
            //创建流程的主表
            //存放主表字段的数组
            WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[2];
            //添加区域经理字段
            bb.writeLog("零售经理：" + strLsjl);
            wrti[0] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[0].setFieldName("lsjl");
            //赋值
            wrti[0].setFieldValue(Util.null2String(strLsjl));
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            wrti[0].setFieldOrder(0);
            wrti[0].setMand(false);
            //添加分公司字段
            bb.writeLog("分公司：" + strFgs);
            wrti[1] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[1].setFieldName("fgs");
            //赋值
            wrti[1].setFieldValue(Util.null2String(strFgs));
            wrti[1].setView(true);
            wrti[1].setEdit(true);
            wrti[1].setFieldOrder(0);
            wrti[1].setMand(false);




            WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
            wrtri[0] = new WorkflowRequestTableRecord();
            wrtri[0].setWorkflowRequestTableFields(wrti);
            WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
            wmi.setRequestRecords(wrtri);




            //通过分公司值获取对应CRM流程的workflowid的sql
            String strGradeSql = "select dckhlccrm from uf_khpflcpz where fgs='"+strFgs+"'";
            bb.writeLog("获取workflowid的sql："+strGradeSql);
            rsGrade.executeQuery(strGradeSql);
            //判断是否有对应workflowid
            String strGradeWfId ="";
            if(rsGrade.next()){
                strGradeWfId=Util.null2String(rsGrade.getString("dckhlccrm"));
                bb.writeLog("CRM流程的workflowid："+strGradeWfId);
            }
            //获取店铺信息表中店长的数据的sql
            String strDcNameSql ="select dc ,dp from uf_dpxx where   ( sfdpphd=1  )  and  fgs='"+strFgs+"' and lsjl='"+strLsjl+"' and dc is not null";
            rsDc.executeQuery(strDcNameSql);

            //获取明细表长度的sql
            String strClerkNumSql="select count(dc) as num  from uf_dpxx where    ( sfdpphd=1  )  and  fgs='"+strFgs+"' and lsjl='"+strLsjl+"' and dc is not null";
            bb.writeLog("获取明细表长度的sql："+strClerkNumSql);
            rsClerkNum.executeQuery(strClerkNumSql);
            //明细表长度
            int intDetailNum=0;
            if(rsClerkNum.next()){
                intDetailNum=Integer.parseInt(Util.null2String(rsClerkNum.getString("num")));
                bb.writeLog("明细表的长度："+intDetailNum);
            }
            //创建流程明细表
            //明细表个数为1
            WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
            //限定明细表的长度
            wrtri = new WorkflowRequestTableRecord[intDetailNum];
            //循环参数
            int intNum=0;
            while (rsDc.next()){
                String strDcName=Util.null2String(rsDc.getString("dc"));

                //获取明细表数据的sql
                String strClerkSql="select id , jobtitle,workcode from hrmresource where  id= '"+strDcName+"'";
                bb.writeLog("获取明细数据的sql:"+strClerkSql);
                rsClerk.executeQuery(strClerkSql);
                if(rsClerk.next()) {
                    //获取店长姓名
                    String strClerkName = Util.null2String(rsClerk.getString("id"));
                    //获取店长工号
                    String strClerkCode = Util.null2String(rsClerk.getString("workcode"));
                    //获取店长岗位
                    String strClerkJob = Util.null2String(rsClerk.getString("jobtitle"));

                    //有8个字段长度为8
                    wrti = new WorkflowRequestTableField[8];

                    //添加店长姓名字段
                    bb.writeLog("店员姓名：" + strClerkName);
                    wrti[0] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[0].setFieldName("xm");
                    //赋值
                    wrti[0].setFieldValue(strClerkName);
                    wrti[0].setView(true);
                    wrti[0].setEdit(true);

                    //添加店长工号字段
                    bb.writeLog("店员工号：" + strClerkCode);
                    wrti[1] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[1].setFieldName("gh");
                    //赋值
                    wrti[1].setFieldValue(strClerkCode);
                    wrti[1].setView(true);
                    wrti[1].setEdit(true);

                    //添加店长岗位字段
                    bb.writeLog("店员岗位：" + strClerkJob);
                    wrti[2] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[2].setFieldName("zw");
                    //赋值
                    wrti[2].setFieldValue(strClerkJob);
                    wrti[2].setView(true);
                    wrti[2].setEdit(true);

                    //添加店长分公司字段
                    bb.writeLog("店员分公司：" + strFgs);
                    wrti[3] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[3].setFieldName("fgs");
                    //赋值
                    wrti[3].setFieldValue(strFgs);
                    wrti[3].setView(true);
                    wrti[3].setEdit(true);

                    //添加店长店铺名称字段
                    bb.writeLog("店员店铺名称：" + strDpName);
                    wrti[4] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[4].setFieldName("dpmc");
                    //赋值
                    wrti[4].setFieldValue(Util.null2String(rsDc.getString("dp")));
                    wrti[4].setView(true);
                    wrti[4].setEdit(true);

                    //添加店长级别字段
                    bb.writeLog("店铺级别：" + strRank);
                    wrti[5] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[5].setFieldName("dpjb");
                    //赋值
                    wrti[5].setFieldValue(strRank);
                    wrti[5].setView(true);
                    wrti[5].setEdit(true);

                    //添加店长状态字段
                    bb.writeLog("店铺状态：" + strStauts);
                    wrti[6] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[6].setFieldName("dpzt");
                    //赋值
                    wrti[6].setFieldValue(strStauts);
                    wrti[6].setView(true);
                    wrti[6].setEdit(true);

                    //添加品牌字段
                    bb.writeLog("品牌：" + strBrand);
                    wrti[7] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[7].setFieldName("pp");
                    //赋值
                    wrti[7].setFieldValue(strBrand);
                    wrti[7].setView(true);
                    wrti[7].setEdit(true);

                    wdti[0] = new WorkflowDetailTableInfo();
                    wdti[0].setWorkflowRequestTableRecords(wrtri);


                    wrtri[intNum] = new WorkflowRequestTableRecord();
                    wrtri[intNum].setWorkflowRequestTableFields(wrti);
                    intNum++;
                }
            }

            //基本信息
            WorkflowBaseInfo wbi = new WorkflowBaseInfo();
            //流程的workflowid
            wbi.setWorkflowId(strGradeWfId);


            WorkflowRequestInfo wri = new WorkflowRequestInfo();
            //查询crm对应创建人的sql
            String strPfrSql="select  pfr from uf_khpflcpfrpz  where  pflx='0'";
            rsPfr.executeQuery(strPfrSql);
            String strPfrName="";
            if(rsPfr.next()){
                 strPfrName = rsPfr.getString("pfr");
                bb.writeLog("CRM对应创建人："+strPfrName);
                //创建人为CRM对应评分人
                wri.setCreatorId(strPfrName);
            }
            //紧急程度
            wri.setRequestLevel("0");
            //请求的名称
            wri.setRequestName("店长CRM考核评分流程");
            //停留在提交节点
            wri.setIsnextflow("0");

            //主表数据
            wri.setWorkflowMainTableInfo(wmi);
            wri.setWorkflowBaseInfo(wbi);


            //明细表数据
            wri.setWorkflowDetailTableInfos(wdti);
            WorkflowServiceImpl wfsi = new WorkflowServiceImpl();

            String strRequestid= wfsi.doCreateWorkflowRequest(wri, Integer.parseInt(strPfrName));
            bb.writeLog("生成流程的requestid:"+strRequestid);


        }


    }


}
